using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Controls
{
    public partial class MyTimeControl : UserControl, IComponentConnector
    {
        public delegate void OnValueChanged(object sender, DependencyPropertyChangedEventArgs args);

        public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(MyTimeControl), new UIPropertyMetadata(double.MinValue));

        public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(MyTimeControl), new PropertyMetadata(double.MaxValue));

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(MyTimeControl), new UIPropertyMetadata(0.0, OnInternalValueChanged));

        public double MinValue
        {
            get
            {
                return (double)GetValue(MinValueProperty);
            }
            set
            {
                SetValue(MinValueProperty, value);
            }
        }

        public double MaxValue
        {
            get
            {
                return (double)GetValue(MaxValueProperty);
            }
            set
            {
                SetValue(MaxValueProperty, MaxValue);
            }
        }

        public double Value
        {
            get
            {
                return (double)GetValue(ValueProperty);
            }
            set
            {
                if (value > MaxValue)
                {
                    value = MaxValue;
                }
                if (value < MinValue)
                {
                    value = MinValue;
                }
                double value2 = Value;
                SetValue(ValueProperty, value);
                if (this.ValueChanged != null)
                {
                    this.ValueChanged(this, new DependencyPropertyChangedEventArgs(ValueProperty, value2, value));
                }
            }
        }

        public event OnValueChanged ValueChanged;

        public MyTimeControl()
        {
            InitializeComponent();
            DataObject.AddPastingHandler(this, DateTextBox_Paste);
            hours.Type = TimeType.Hours;
            min.Type = TimeType.Min;
            sec.Type = TimeType.Sec;
            hours.setNeighbour(null, min);
            min.setNeighbour(hours, sec);
            sec.setNeighbour(min, hours);
        }

        private static void OnInternalValueChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            NumericSpan numericSpan = sender as NumericSpan;
            if (numericSpan != null)
            {
                numericSpan.Value = numericSpan.Value;
            }
        }

        private void DateTextBox_Paste(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(string)))
            {
                string date = e.DataObject.GetData(typeof(string)).ToString();
                setDate(date);
            }
            e.CancelCommand();
        }

        public double getTime()
        {
            DateTime dateTime = new DateTime(1, 1, 1, int.Parse((hours.Text == string.Empty) ? "0" : hours.Text), int.Parse((min.Text == string.Empty) ? "0" : min.Text), int.Parse((sec.Text == string.Empty) ? "0" : sec.Text));
            DateTime dateTime2 = new DateTime(1, 1, 1);
            return (dateTime - dateTime2).TotalSeconds;
        }

        public bool setDate(string strDate)
        {
            string[] array = strDate.Split(':');
            if (array.Length == 3)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    int result;
                    if (!int.TryParse(array[i], out result) || result < 0)
                    {
                        return false;
                    }
                }
                int result2;
                int.TryParse(array[1], out result2);
                int result3;
                int.TryParse(array[1], out result3);
                if (result2 > 60 || result3 > 60)
                {
                    return false;
                }
                hours.Text = array[0];
                min.Text = array[1];
                sec.Text = array[2];
                return true;
            }
            return false;
        }

        private void time_LostFocus(object sender, RoutedEventArgs e)
        {
            Value = getTime();
        }

        [DebuggerNonUserCode]
        [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
        internal Delegate _CreateDelegate(Type delegateType, string handler)
        {
            return Delegate.CreateDelegate(delegateType, this, handler);
        }
    }
}
